package org.jeecg.modules.patrolLine.service.impl;

import org.jeecg.modules.patrolLine.entity.AgPatrolLine;
import org.jeecg.modules.patrolLine.entity.AgPatrolPoint;
import org.jeecg.modules.patrolLine.mapper.AgPatrolPointMapper;
import org.jeecg.modules.patrolLine.mapper.AgPatrolLineMapper;
import org.jeecg.modules.patrolLine.service.IAgPatrolLineService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 巡检线路
 * @Author: jeecg-boot
 * @Date:   2020-05-25
 * @Version: V1.0
 */
@Service
public class AgPatrolLineServiceImpl extends ServiceImpl<AgPatrolLineMapper, AgPatrolLine> implements IAgPatrolLineService {

	@Autowired
	private AgPatrolLineMapper agPatrolLineMapper;
	@Autowired
	private AgPatrolPointMapper agPatrolPointMapper;
	
	@Override
	@Transactional
	public void saveMain(AgPatrolLine agPatrolLine, List<AgPatrolPoint> agPatrolPointList) {
		agPatrolLineMapper.insert(agPatrolLine);
		if(agPatrolPointList!=null && agPatrolPointList.size()>0) {
			for(AgPatrolPoint entity:agPatrolPointList) {
				//外键设置
				entity.setPatrolId(agPatrolLine.getId());
				agPatrolPointMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(AgPatrolLine agPatrolLine,List<AgPatrolPoint> agPatrolPointList) {
		agPatrolLineMapper.updateById(agPatrolLine);
		
		//1.先删除子表数据
		agPatrolPointMapper.deleteByMainId(agPatrolLine.getId());
		
		//2.子表数据重新插入
		if(agPatrolPointList!=null && agPatrolPointList.size()>0) {
			for(AgPatrolPoint entity:agPatrolPointList) {
				//外键设置
				entity.setPatrolId(agPatrolLine.getId());
				agPatrolPointMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		agPatrolPointMapper.deleteByMainId(id);
		agPatrolLineMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			agPatrolPointMapper.deleteByMainId(id.toString());
			agPatrolLineMapper.deleteById(id);
		}
	}
	
}
